package com.tiantian.quanminxiu.presenter;

import android.util.Log;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.MainRankingView;

import org.json.JSONArray;
import org.json.JSONObject;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by admin on 2016/8/11.
 */
public class MainRankingPresent implements Presenter<MainRankingView> {

    private MainRankingView mainRankingView;

    @Override
    public void attachView(MainRankingView view) {
        this.mainRankingView = view;
    }

    @Override
    public void detachView() {
        mainRankingView = null;
    }

    //type =1 ;用户列表 type = 2 主播列表
    public void setRankingList(int type,String sort,String col){
        mainRankingView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(mainRankingView.getContext());
        AnchorService service = app.getAnchorService();
        if(type==1){
            service.user_userinf_page(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, "user.user-info.page",
                    true, true, 0, 0, 0, 10,sort+"_"+col)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(app.defaultSubscribeScheduler())
                    .finallyDo(()->{
                        mainRankingView.Loading_Close();
                    })
                    .subscribe(str -> {
                        Log.i("dd","--rankuser:"+str.toString());
                        mainRankingView.setRankingList(str.optJSONObject("data").optJSONArray("aaData"));
                    }, throwable -> {
                        CommonUtils.showToask(mainRankingView.getContext(), throwable.getMessage());
                    });
        }else if(type == 2){
            service.user_useranchor_page(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, "user.user-anchor.page",
                    true, true, 0, 0, 0, 10,sort+"_"+col)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(app.defaultSubscribeScheduler())
                    .finallyDo(()->{
                        mainRankingView.Loading_Close();
                    })
                    .subscribe(str -> {
                        Log.i("dd", "--rankanchor:" + str.toString());
                        mainRankingView.setRankingList(str.optJSONArray("aaData"));
                    }, throwable -> {
                        CommonUtils.showToask(mainRankingView.getContext(), throwable.getMessage());
                    });
        }
    }

    private int i=0;
    public void setWeekStartList(int Type,int Week){
        /*Mobile_ID,
                APP_Type,
                Sign,
        Gift_ID//周星礼物ID
        Length//请求个数
        Type//请求类型：1-艺人周星榜，2-粉丝周星榜
        Week //请求类型：1-本周， 2-上周, 3-大上周， 4-大大上周， 5-特殊日期
        StartTime: //传入特殊日开始时间
        EndTime: //传入特殊日期结束时间*/
        mainRankingView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(mainRankingView.getContext());
        AnchorService service = app.getAnchorService();
        JSONArray WeekSartlist = new JSONArray();
        i=0;
        if(UserData.Current.WeekStar.length()>i){
            JSONObject object1 = UserData.Current.WeekStar.optJSONObject(i);
            service.weekstartrank(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE,object1.optInt("id")+"",3,Type,Week,"","")
                    .flatMap(str -> {
                        Log.i("dd", "--weekstart:" + str);
                        i++;
                        if (UserData.Current.WeekStar.length() > i) {
                            WeekSartlist.put(str.optJSONObject("data"));
                            JSONObject o = UserData.Current.WeekStar.optJSONObject(i);
                            return service.weekstartrank(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, o.optInt("id") + "", 3, Type, Week, "", "");
                        } else {
                            return Observable.just(str);
                        }
                    }).flatMap(str -> {
                Log.i("dd", "--weekstart:" + str);
                i++;
                if (UserData.Current.WeekStar.length() > i) {
                    WeekSartlist.put(str.optJSONObject("data"));
                    JSONObject o = UserData.Current.WeekStar.optJSONObject(i);
                    return service.weekstartrank(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, o.optInt("id") + "", 3, Type, Week, "", "");
                } else {
                    return Observable.just(str);
                }
            }).flatMap(str -> {
                Log.i("dd", "--weekstart:" + str);
                i++;
                if (UserData.Current.WeekStar.length() > i) {
                    WeekSartlist.put(str.optJSONObject("data"));
                    JSONObject o = UserData.Current.WeekStar.optJSONObject(i);
                    return service.weekstartrank(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, o.optInt("id") + "", 3, Type, Week, "", "");
                } else {
                    return Observable.just(str);
                }
            }).flatMap(str -> {
                Log.i("dd", "--weekstart:" + str);
                i++;
                if (UserData.Current.WeekStar.length() > i) {
                    WeekSartlist.put(str.optJSONObject("data"));
                    JSONObject o = UserData.Current.WeekStar.optJSONObject(i);
                    return service.weekstartrank(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, o.optInt("id") + "", 3, Type, Week, "", "");
                } else {
                    return Observable.just(str);
                }
            }).flatMap(str -> {
                Log.i("dd", "--weekstart:" + str);
                i++;
                if (UserData.Current.WeekStar.length() > i) {
                    WeekSartlist.put(str.optJSONObject("data"));
                    JSONObject o = UserData.Current.WeekStar.optJSONObject(i);
                    return service.weekstartrank(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, o.optInt("id") + "", 3, Type, Week, "", "");
                } else {
                    return Observable.just(str);
                }
            })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(app.defaultSubscribeScheduler())
                    .finallyDo(() -> {
                        mainRankingView.Loading_Close();
                    })
                    .subscribe(str -> {
                        Log.i("dd", "--rankuser:" + str.toString());
                        WeekSartlist.put(str.optJSONObject("data"));
                        mainRankingView.setWeekStartList(WeekSartlist);
                        Log.i("dd", "--WeekSartlist:" + WeekSartlist.toString());
                    }, throwable -> {
                        CommonUtils.showToask(mainRankingView.getContext(), throwable.getMessage());
                    });
        }
    }

}
